float4x4 ViewProjection;

float3 LightPosition = float3(15, 100, -25);
float3 DiffuseLight = 1;
float3 AmbientLight = 0.15;

float3 CameraPosition;

float FogFar = 768;
float FogNear = 512;
float4 FogColor = {0.43, 0.5, .56, 1};

texture Texture;

sampler Sampler = sampler_state
{
	Texture = (Texture);
	MinFilter = Point;
    MagFilter = Point;

    AddressU = Clamp;
    AddressV = Clamp;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
	float4 Normal : NORMAL0;
	float2 TextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
	float2 TextureCoordinate : TEXCOORD0;
	float3 Normal : TEXCOORD1;
	float3 Position3D : TEXCOORD2;
	float Distance : TEXCOORD3;
};

float DotProduct(float3 lightPos, float3 pos3D, float3 normal)
{
    return dot(-normalize(pos3D - lightPos), normal);    
}

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
	
    output.Position = mul(input.Position, ViewProjection);
	output.TextureCoordinate = input.TextureCoordinate;
	output.Position3D = input.Position;
	output.Normal = input.Normal;
	output.Distance = length(CameraPosition - input.Position);
    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
	float lightFactor = DotProduct(LightPosition, input.Position3D, input.Normal);
	float fog = saturate((input.Distance - FogNear) / (FogNear - FogFar));
	lightFactor = saturate(lightFactor);
	lightFactor *= DiffuseLight;
    float4 color = tex2D(Sampler, input.TextureCoordinate) * float4((lightFactor + AmbientLight), 1);

	return lerp(FogColor, color, fog);
}

technique StaticVertexBufferRendering
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
